package model

import "gorm.io/gorm/clause"

type Frp struct {
	Id          string	`xml:"-" json:"id" gorm:"column:id;primary_key;not null;index"`
	Name        string	`xml:"-" json:"name"`
	Type        string	`xml:"-" json:"type"`
	Local_ip    string	`xml:"-" json:"local_ip"`
	Local_port  *int	`xml:"-" json:"local_port"`
	Remote_port *int	`xml:"-" json:"remote_port"`
}
func FindFrps(filter *Frp)[]Frp{
	var kps []Frp
	if err:=sqldb.Find(&kps,filter).Error;err != nil{
		return nil
	}else{
		return kps
	}
}
func DeleteFrps()(int64,error){
	var kps []Frp
	sqldb.Find(&kps)
	index :=0
	for i:=0; i < len(kps); i++{
		if _,err := kps[i].Delete();err != nil{
			return int64(index),err
		}
		index += 1
	}
	return int64(index),nil
}

func (p* Frp) Insert()error{
	return sqldb.Create(p).Error
}
func (p* Frp) Save()(int64,error){
	ret := sqldb.Clauses(clause.OnConflict{
		Columns:   []clause.Column{{Name: "id"}},
		DoUpdates: clause.AssignmentColumns([]string{"name", "type","local_ip","local_port","remote_port"}),
	}).Create(p)
	return ret.RowsAffected,ret.Error
}
func (p* Frp) Delete()(int64,error){
	db :=sqldb.Where(&Frp{Id:p.Id}).Delete(&Frp{})
	return db.RowsAffected,db.Error
}